$(document).ready(function() {
    ads_fileupload();
    addads_fileupload();
    get_hash();
    $("#submitAds").click(function(){
    	saveAds();
    });
	$("#add_ads_form").click(function(){
		addAdsForm();
	});
	$("#add_addsubmitAds").click(function(){
		addAds();
	});
});
function ads(){
	var asort = 0;
	ads_icons = {};
	xfunc = "ads/get_ads_list";
	
	data_sent = {};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request
				$(result.intID).each(function(k,v){
					asort = result.intID[k];
					ads_icons_panel = '';
						ads_icons_panel +='<tr>';;
							ads_icons_panel +='<td><img width="86" class="img-rounded" height="66" src="'+base_url+'assets/uploaded/img/thumbnail/'+result.strImgLink[k]+'" alt=""/></td>';
							ads_icons_panel +='<td>'+result.strAddName[k]+'</td>';
							ads_icons_panel +='<td>'+result.strDescription[k]+'</td>';
							ads_icons_panel +='<td>'+result.strNotes[k]+'</td>';
							ads_icons_panel +='<td class="controls"><div class="btn-group">';
							ads_icons_panel +='<button class="btn btn-primary" onclick="javascript:editAds(\''+result.intID[k]+'\')"><i class="icon-edit"></i>Edit</button>';
							ads_icons_panel +='<button class="btn btn-danger" onclick="javascript:delAds(\''+result.intID[k]+'\', \''+result.strAddName[k]+'\')"><i class="icon-trash"></i>Delete</button>';
							ads_icons_panel +='</td>';
						ads_icons_panel +='</tr>';
						ads_icons[result.intID[k]]=ads_icons_panel;
				});
				
				ads_html_panel = '<table class="table table-striped table-bordered" id="ads_table">';
				ads_html_panel += '<thead>';
					ads_html_panel += '<tr>';
						ads_html_panel += '<th class="thumb-photo">Thumbnail</th>';
						ads_html_panel += '<th class="note-title">Ads Name</th>';
						ads_html_panel += '<th class="note-extra">Ads Type</th>';
						ads_html_panel += '<th class="note-content">Ads Link</th>';
						ads_html_panel += '<th>Control</th>';
					ads_html_panel += '</tr>';
				ads_html_panel += '</thead>';
				ads_html_panel += '<tbody>';
				
				for(x=0;x<=asort;x++){
					if($.trim(ads_icons[x])!=""){
						ads_html_panel +=ads_icons[x];
					}
								
				}		
				ads_html_panel += '</tbody>';		
				ads_html_panel += '</table>';
				
				$('#ads_content').html(ads_html_panel);
				$('#ads_table').dataTable({
					"sPaginationType": "full_numbers",
					"bLengthChange": false,
					"bSort": false
				});
			}
		});	
}
function addAdsForm(){
	$("#ads_content, #add_ads_form").hide();
	$("#addAds").slideDown('slow', function(){
		$("#ads_content").hide();
		$("#addAds").slideDown('slow', function(){
			$(".back-btn-addads a").attr("onclick","btn_back_addads()");
		});	
	});	
}
function addAds(){
	var adsType = $("#add_adsType").val();
	var adsDelay = $("#add_adsDelay").val();
	var adsName = $("#add_adsName").val();
	var adsNote = $("#add_adsNote").val();
	var strImgLink = $("#addads_thumbnailLink").val();
	
	if(adsDelay=='') {
		$("#errorAds").html("<div class='alert alert-error'>Error: Put the rotation delay!</div>");
	} else if(adsName=='') {
		$("#errorAds").html("<div class='alert alert-error'>Error: Put the ads name!</div>");
	} else if(adsNote=='') {
		$("#errorAds").html("<div class='alert alert-error'>Error: Put the ads note!</div>");
	} else if(strImgLink=='') {
		$("#errorAds").html("<div class='alert alert-error'>Error: Put the image banner!</div>");
	} else {
		xfunc = "ads/save_ads";
		data_sent = {
				'adsType':adsType,
				'adsDelay':adsDelay,
				'adsName':adsName,
				'adsNote':adsNote,
				'strImgLink':strImgLink
		};
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				$("#errorAds").hide();
				$("#addadsSuccess").html('<div class="alert alert-success">Data was successfully saved!</div>');
				
				$("#add_adsType").val('');
				$("#add_adsDelay").val('');
				$("#add_adsName").val('');
				$("#add_adsNote").val('');
				$("#addads_thumbnailLink").val('');
				$(".alert-success").fadeOut(5000);
			}
		});
	}
}
function editAds(noteID){
	xfunc = "ads/get_ads";
	data_sent = {
			'intID':noteID
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			var ads_thumbnail = '<div class="imgThumbnail"><img width="86" class="img-rounded" src="'+base_url+'assets/uploaded/img/thumbnail/'+result.strImgLink+'" alt=""/></div>';
			$("#intAdsID").val(result.intID);
			$("#adsName").val(result.strAddName);
			$("#adsNote").val(result.strNotes);
			$("#adsDelay").val(result.intMSDelay);
			$("#adsOrder").val(result.intOrder);
			$("#ads_thumbnail").html(ads_thumbnail);
			$("#ads_thumbnailLink").val(result.strImgLink);
		}
	});

	$("#add_ads_form").hide();
	$("#ads_content").hide();
	$("#editAds").slideDown('slow', function(){
		$(".back-btn-ads a").attr("onclick","btn_back_ads()");
	});	
}

function saveAds(){
	var intID = $("#intAdsID").val();
	var intMSDelay = $("#adsDelay").val();
	var intOrder = $("#adsOrder").val();
	var strAddName = $("#adsName").val();
	var intType = $("#adsType").val();
	var strNotes = $("#adsNote").val();
	var strImgLink = $("#ads_thumbnailLink").val();
	
	xfunc = "ads/save_ads";
	data_sent = {
			'intID':intID,
			'intMSDelay':intMSDelay,
			'intOrder':intOrder,
			'intType':intType,
			'strAddName':strAddName,
			'strNotes':strNotes,
			'strImgLink':strImgLink
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			$("#adsSuccess").html('<div class="alert alert-success">Data was successfully updated!</div>');
			$(".alert-success").fadeOut(5000);
		}
	});
}
function delAds(noteID, titleName){
	xfunc = "ads/delete_ads";
	data_sent = {
			'intID':noteID
	};
	$('#label-delete').html(titleName);
    $('#deleteModal').modal('show');
    $('button#confirm').click(function(e){
        $('#deleteModal').modal('hide');
		
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				ads();
			}
		});
    });
}
function btn_back_addads(){
	$("#addAds").hide();
	$("#ads_content").slideDown('slow', function(){
		$("#add_ads_form").show();
		ads();
	});	
}
function btn_back_ads(){
	$("#editAds").hide();
	$("#ads_content").slideDown('slow', function(){
		$("#add_ads_form").show();
		ads();
	});	
}

function ads_fileupload(){
	$('#ads_input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/index.php');
	adsfile_upload();
}
function adsfile_upload(){
	$('.ads_fileupload').fileupload();
	$('.ads_fileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					html = '<img width="86" class="img-rounded" src="'+base_url+"assets/uploaded/img/thumbnail/"+v.name+'" />';
					$("#ads_thumbnailLink").val(v.name);			
					$(".ads_imgThumbnail").hide();
					$(".imgThumbnail").slideDown('slow', function(){
						$(".imgThumbnail").html(html);
					});					
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$(".result-message-box").html("Upload Error!");
	});		
}
function addads_fileupload(){
	$('#addads_input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/index.php');
	addadsfile_upload();
}
function addadsfile_upload(){
	$('.addads_fileupload').fileupload();
	$('.addads_fileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					html = '<img width="86" class="img-rounded" src="'+base_url+"assets/uploaded/img/thumbnail/"+v.name+'" />';
					$("#addads_thumbnailLink").val(v.name);				
					$("#addAds_thumbnail").html(html);			
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$(".result-message-box").html("Upload Error!");
	});		
}